package com.github.ilioili.justdoit.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.taihe.template.base.util.SizeUtil;

/**
 * Created by User on 16/6/28.
 */
public class AxisChart extends View {

    public static final float SCALE_CACULATOR_SEED = 0.8f;
    private int cx;
    private int cy;
    private int w;
    private int h;
    private Paint paint;
    private float[] data = new float[]{20, 30, 10, 50, 60, 80, 90, 70, 80, 85, 87, 80, 78, 65};
    private Path path;
    private int dotRadius;

    public AxisChart(Context context) {
        this(context, null);
    }

    public AxisChart(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AxisChart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(SizeUtil.dpToPx(1, context));
        dotRadius = SizeUtil.dpToPx(1, context);
        path = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        cx = getPaddingLeft();
        cy = h - getPaddingBottom();
        this.w = w;
        this.h = h;
        computePath();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect((float) cx, (float) getPaddingTop(), (float) (w - getPaddingRight()), (float) cy, paint);
        float interval = 1f * (w - getPaddingRight() - getPaddingLeft()) / (data.length - 1);
        float scale = (h - getPaddingTop() - getPaddingBottom()) / getMax(data) * SCALE_CACULATOR_SEED;
        for (int i = 0; i < data.length; i++) {
            if (i < data.length) {
                float nextPointX = cx + i * interval;
                float nextPointY = cy - scale * data[i];
                canvas.drawCircle(nextPointX, nextPointY, dotRadius, paint);
            }
        }
        canvas.drawPath(path, paint);
    }


    private float getMax(float[] data) {
        float max = data[0];
        for (float f : data) {
            if (f > max) max = f;
        }
        return max;
    }

    public void setData(float[] data) {
        this.data = data;
        if (w * h != 0) {
            computePath();
        }
        invalidate();
    }

    public void setData(int[] data) {
        this.data = new float[data.length];
        for (int i = 0; i < data.length; i++) {
            this.data[i] = data[i];
        }
        setData(this.data);
    }

    private void computePath() {
        float interval = 1f * (w - getPaddingRight() - getPaddingLeft()) / (data.length - 1);
        float scale = (h - getPaddingTop() - getPaddingBottom()) / getMax(data) * SCALE_CACULATOR_SEED;
        path.rewind();
        path.moveTo(cx, cy);
        path.lineTo(cx, cy - data[0] * scale);
        for (int i = 0; i < data.length; i++) {
            if (i < data.length - 1) {
                float controlX1 = cx + (i + 0.5f) * interval;
                float controlY1 = cy - scale * data[i];
                float controlX2 = cx + (i + 0.5f) * interval;
                float controlY2 = cy - scale * data[i + 1];
                float nextPointX = cx + (i + 1) * interval;
                float nextPointY = cy - scale * data[i + 1];
                path.cubicTo(controlX1, controlY1, controlX2, controlY2, nextPointX, nextPointY);
            }
        }
        path.lineTo(w - getPaddingRight(), cy);
        path.close();
    }

}
